package cn.gov.msa.collection;

import java.util.ArrayList;
import java.util.List;

import cn.gov.msa.util.PropertiesTools;
import cn.gov.msa.util.WebServiceTools;
import cn.msa.gov.model.Role;
import cn.msa.gov.model.UserDetail;
import net.sf.json.JSONArray;
import net.sf.json.JSONObject;

public class AppLdapService {

	private static String  url = PropertiesTools.readProperties("app.url");
	
	public static UserDetail getUserDetail(String token, Integer isrole) throws Exception{
		UserDetail userDetail = new UserDetail();
		
		JSONObject param = new JSONObject();
		
		param.put("tokenString", token);
		if(0==isrole)
			param.put("isUserInfo", "0");
		
        String[] parameter = new String[]{param.toString()};      
    	JSONObject jsonResult = JSONObject.fromObject(WebServiceTools.getDatasByUrl(url, "checkOAMToken", parameter));
    	if(!jsonResult.isEmpty()) {
    		if("验证成功".equals(jsonResult.getString("success")) && jsonResult.getJSONObject("datas").getJSONObject("table").has("row")){
        		JSONObject jsonUser = jsonResult.getJSONObject("datas").getJSONObject("table").getJSONObject("row");
        		userDetail.setUserId(jsonUser.getString("uid"));
        		userDetail.setUserName(jsonUser.getString("userName"));
        		userDetail.setProposerId(jsonUser.getString("uid"));
        		userDetail.setProposer(jsonUser.getString("userName"));
        		if(jsonUser.has("telephoneNumber"))
        			userDetail.setTelePhone(jsonUser.getString("telephoneNumber"));
        		if(jsonUser.has("linkAddress"))
        			userDetail.setContactAddr(jsonUser.getString("linkAddress"));
        		userDetail.setEmail(jsonUser.getString("email"));
        		userDetail.setMobilePhone(jsonUser.optString("mobile"));
        		if(jsonUser.has("orgCode")) {
        			userDetail.setCompanyId(jsonUser.getString("orgCode"));
        		}
        		if(jsonUser.has("table")) {
    	    		JSONArray tables = jsonUser.getJSONArray("table");
    				for (int i = 0; i < tables.size(); i++) {
    					JSONObject table = tables.getJSONObject(i);
    					if("org".equals(table.getString("tableName"))) {
    						JSONArray orgResult = table.getJSONArray("row");
    						for (int j = 0; j <orgResult.size(); j++) {
    							JSONObject org = orgResult.getJSONObject(j);
    							if(org.has("orgName")) {
    								userDetail.setCompany(org.getString("orgName"));
    							}else {
    								userDetail.setCompany(jsonUser.getString("orgCode"));
    							}
    						}
    					}else if("role".equals(table.getString("tableName"))) {
    						JSONArray rows = table.getJSONArray("row");
    						List<Role> roles = new ArrayList<Role>();
    						for (int j = 0; j < rows.size(); j++) {
    							JSONObject row = rows.getJSONObject(j);
    							Role role = new Role();
    							role.setRolecode(row.optString("roleCode"));
    							role.setRolename(row.optString("roleName"));
    							roles.add(role);
    						}
    						userDetail.setRoles(roles);
    					}
    				}
        		}
        	}
    	}
    	return userDetail;
	}

}
